package com.ihr360.payroll.dao;

import com.ihr360.payroll.model.dto.staff.StaffPayrollInfoJournal;
import com.ihr360.payroll.model.vo.StaffPayrollInfoJournalVO;
import com.ihr360.payroll.type.SalaryProfileChangeType;
import com.ihr360.rest.reposity.SimpleRestReposity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@RepositoryRestResource
public interface StaffPayrollInfoJournalRepository extends SimpleRestReposity<StaffPayrollInfoJournal, Long> {

    /**
     * 获取相关员工和字段最终修改的记录
     *
     * @param companyId
     * @param staffIds
     * @return
     */
    @Query(value = " select new com.ihr360.payroll.model.vo.StaffPayrollInfoJournalVO(staffId,fieldCode,max(createdAt))  from StaffPayrollInfoJournal j " +
            " where staffId in (:staffIds) and companyId = :companyId" +
            " and isDeleted is not true " +
            " group by staffId,fieldCode ")
    List<StaffPayrollInfoJournalVO> findFinalUpdateJournal(@Param("companyId") String companyId, @Param("staffIds") Set<String> staffIds);

    //findByCompanyIdAndStaffIdAndFieldCodeAndNewValueAndEffectiveAt

    /**
     * 超找当前生效薪资档案的Journal
     *
     * @param companyId
     * @param staffId
     * @param code
     * @param newValue
     * @param currentDay
     * @param changeType
     * @return
     */
    @Query(value = " select j from StaffPayrollInfoJournal  j " +
            " where companyId = :companyId " +
            " and staffId = :staffId " +
            " and fieldCode = :code " +
//            " and newValue = :newValue " +
            " and effectiveAt <= :currentDay " +
            " and changeType = :changeType " +
            " and isDeleted is not true  ")
    Page<StaffPayrollInfoJournal> findCurrentEffectiveJournal(@Param("companyId") String companyId,
                                                              @Param("staffId") String staffId,
                                                              @Param("code") String code,
//                                                              @Param("newValue") String newValue,
                                                              @Param("currentDay") Date currentDay,
                                                              @Param("changeType") SalaryProfileChangeType changeType,
                                                              Pageable pageable);

    /**
     * 上一次生效的Journal
     *
     * @param companyId
     * @param staffId
     * @param code
     * @param currentCreatedAt
     * @param currentDay       @return
     */
    @Query(value = " select j from StaffPayrollInfoJournal j " +
            " where companyId = :companyId " +
            " and staffId = :staffId " +
            " and fieldCode = :code " +
            " and newValue != :newValue " +
//            " and createdAt < :currentCreatedAt " +
            " and effectiveAt < :currentDay " +
            " and isDeleted is not true   ")
    Page<StaffPayrollInfoJournal> findPreEffectiveJournal(@Param("companyId") String companyId,
                                                          @Param("staffId") String staffId,
                                                          @Param("code") String code,
                                                          @Param("newValue") String newValue,
                                                          @Param("currentDay") Date currentDay,
//                                                          @Param("currentCreatedAt") Date currentCreatedAt,
                                                          Pageable pageable);


    /**
     * 下一次生效的Journal
     *
     * @param companyId
     * @param staffId
     * @param code
     * @param newValue
     * @param currentCreatedAt
     * @param currentDay       @return
     */
    @Query(value = " select j from StaffPayrollInfoJournal j " +
            " where companyId = :companyId " +
            " and staffId = :staffId " +
            " and fieldCode = :code " +
//            " and newValue != :newValue " +   //允许两次修改值一致
            " and createdAt >= :currentCreatedAt " +
            " and effectiveAt > :currentDay " +
            "  and isDeleted is not true   " +
            " order by createdAt ")
    List<StaffPayrollInfoJournal> findNextEffectiveJournal(@Param("companyId") String companyId,
                                                           @Param("staffId") String staffId,
                                                           @Param("code") String code,
//                                                    @Param("newValue") String newValue,
                                                           @Param("currentDay") Date currentDay,
                                                           @Param("currentCreatedAt") Date currentCreatedAt);

    Page<StaffPayrollInfoJournal> findByCompanyId(String companyId, Pageable pageable);

    List<StaffPayrollInfoJournal> findByCompanyIdAndStaffId(String companyId, String staffId);

    List<StaffPayrollInfoJournal> findByCompanyIdAndStaffIdInAndIsDeleted(String companyId, List<String> staffIds, Boolean isDeleted);

    List<StaffPayrollInfoJournal> findByCompanyIdAndStaffIdIn(String companyId, List<String> staffIds);

    /**
     * 查询指定时间区间内发生异动的人员列表
     */
    List<StaffPayrollInfoJournal> findByCompanyIdAndChangeTypeInAndEffectiveAtBetweenAndIsDeleted(String companyId, List<SalaryProfileChangeType> changeTypeList, Date startDate,
                                                                                                  Date endDate, Boolean isDeleted);

    /**
     * 所有通过数据迁移来自Irs的staffId
     *
     * @return
     */
    @Query(value = " select distinct s.staffId from StaffPayrollInfoJournal s where changeType = :changeType and reason = 'i人事数据迁移' and companyId = :companyId ")
    List<String> getAllProbationReviewStaffIdsFromIrs(@Param("changeType") SalaryProfileChangeType changeType, @Param("companyId") String companyId);


}
